﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Shove.Web.UI;

public partial class UserControls_Cart : UCBase
{
    public UserControls_Cart()
    {
        Name = "购物车";

        swpas = new ShoveWebPartAttribute[18];
        swpas[0] = new ShoveWebPartAttribute_Int("控件宽度", "1,1004", 759, "控件宽度");
        swpas[1] = new ShoveWebPartAttribute_Int("导航文本单元格宽度", "1,1000", 139, "导航文本单元格宽度");
        swpas[2] = new ShoveWebPartAttribute_Int("导航高度", "1,300", 23, "导航高度");
        swpas[3] = new ShoveWebPartAttribute_Int("每页显示个数", "1,20", 5, "每页显示个数");
        swpas[4] = new ShoveWebPartAttribute_Int("底部按钮单元格百分比", "1,100", 50, "底部按钮单元格百分比");
        swpas[5] = new ShoveWebPartAttribute_Text("表格CSS样式", "", "black12", "表格CSS样式");
        swpas[6] = new ShoveWebPartAttribute_Text("文本框样式", "", "in_p2", "文本框样式");
        swpas[7] = new ShoveWebPartAttribute_Text("导航文本样式", "", "Black12", "导航文本样式");
        swpas[8] = new ShoveWebPartAttribute_Text("消费金额样式", "", "red", "消费金额样式");

        swpas[9] = new ShoveWebPartAttribute_Image("导航背景图片1", "", "images/UserControls/bg_gold_23.jpg", "导航背景图片1");
        swpas[10] = new ShoveWebPartAttribute_Image("导航背景图片2", "", "images/UserControls/bg_gold_23_2.jpg", "导航背景图片2");
        swpas[11] = new ShoveWebPartAttribute_Image("删除按钮图片", "", "images/UserControls/b-delete.gif", "删除按钮图片");
        swpas[12] = new ShoveWebPartAttribute_Image("清空购物车图片", "", "images/UserControls/button_car_1.jpg", "清空购物车图片");
        swpas[13] = new ShoveWebPartAttribute_Image("继续购物图片", "", "images/UserControls/button_car_2.jpg", "继续购物图片");
        swpas[14] = new ShoveWebPartAttribute_Image("去收银台图片", "", "images/UserControls/button_car_4.jpg", "去收银台图片");
        swpas[15] = new ShoveWebPartAttribute_Image("表头背景图片", "", "images/UserControls/bg_hui_23_2.jpg", "表头背景图片");

        swpas[16] = new ShoveWebPartAttribute_Color("控件边框颜色", "#cccccc", "控件边框颜色");
        swpas[17] = new ShoveWebPartAttribute_Int("表头高度", "1,100", 23, "表头高度");
    }

    private void InitlayOut()
    {
        tabctr.Width = swpAttributes[0];
        tabctr.BgColor = swpAttributes[16];

        //tdsign.Height = swpAttributes[2];
        //tdsign.Width = swpAttributes[1];
        //tdsign.Attributes.Add("class", swpAttributes[7]);
        //tdsign.Style.Add(HtmlTextWriterStyle.BackgroundImage,"url('"+swpAttributes[9]+"')");
        //tdspace.Style.Add(HtmlTextWriterStyle.BackgroundImage, "url('" + swpAttributes[10] + "')");

        gvGood.PageSize = Shove._Convert.StrToInt(swpAttributes[3], 5);
        //gvGood.CssClass = swpAttributes[5];
        gvGood.HeaderStyle.Height = new Unit(swpAttributes[17]);

        tdtotal.Attributes.Add("class", swpAttributes[8]);
        tdBottom.Width = swpAttributes[4] + "%";

        imgdel.Src = "~/" + swpAttributes[12];
        imggood.Src = "~/" + swpAttributes[13];
        ibNext.ImageUrl = "~/" + swpAttributes[14];
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (_Customer == null)
        {
            Response.Redirect("Default.aspx?PN=Login");
        }

        hCartType.Value = Shove._Web.Utility.GetRequest("CartType");    //哪个购物车(会员推荐专区、百企网)

        InitlayOut();
        LoadCart();
    }

    /// <summary>
    /// 加载购物车中的商品
    /// </summary>
    private void LoadCart()
    {
        if (hCartType.Value == "carList2")
        {
            string sum = "0";
            gvGood.DataSource = GoodOfCart.GetItems("carList2", ref sum);
            gvGood.DataBind();
            lbSum.Text = sum;
        }
        else
        {
            string sum = "0";
            gvGood.DataSource = GoodOfCart.GetItems("carList", ref sum);
            gvGood.DataBind();
            lbSum.Text = sum;

        }
    }

    protected void gvGood_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Style.Add("background-image", "url('"+swpAttributes[15]+"')");
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton button = (ImageButton)e.Row.Cells[10].FindControl("ibDelete");
            button.Attributes.Add("onclick", "reMoveOne(" + gvGood.DataKeys[e.Row.RowIndex].Value + ")");
            button.ImageUrl = "~/" + swpAttributes[11];
            Image img = (Image)e.Row.FindControl("imgGoodPic");
            img.ImageUrl = "~/UpFile/Goods/" + img.ImageUrl;

            TextBox txtNum = (TextBox)e.Row.Cells[8].FindControl("txtNum");
            txtNum.CssClass = swpAttributes[6];
        }
    }

    /// <summary>
    /// 去收银台
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibNext_Click1(object sender, ImageClickEventArgs e)
    {        
        if (gvGood.Rows.Count == 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "您的购物车中没有商品！");
        }
        else
        {
            if (hCartType.Value == "carList2")  //哪个购物车(会员推荐专区、百企网)
            {
                Response.Redirect("Default.aspx?PN=Cart2&CartType=carList2");
            }
            else
            {
                Response.Redirect("Default.aspx?PN=Cart2");
            }
        }
    }

    protected void gvGood_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        LoadCart();
    }
}
